package com.whcoding.easypoi.service.impl;

import com.whcoding.easypoi.service.PoiExcelService;
import com.whcoding.easypoi.util.ExportExcelUtils;
import com.whcoding.easypoi.util.LocalJsonUtil;
import com.whcoding.easypoi.vo.OrmDeptVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @program: spring-boot-learning
 * @description:
 * @author: whcoding
 * @create: 2022-09-08 16:21
 **/
@Service
public class PoiExcelServiceImpl implements PoiExcelService {
	/**
	 * 日志
	 */
	private static Logger logger = LoggerFactory.getLogger(PoiExcelServiceImpl.class);

	/**
	 * 导出dept明细
	 *
	 * @param response
	 * @param ormDeptVO
	 */
	@Override
	public void exportDeptDetail(HttpServletResponse response, OrmDeptVO ormDeptVO) {
		//1. 查询deptList数据
		List<OrmDeptVO> deptList = LocalJsonUtil.getListFromJson("json/dept.json", OrmDeptVO.class);
		//2.设置表头
		LinkedHashMap<String, String> headAlias = new LinkedHashMap<>();
		headAlias.put("id", "序号");
		headAlias.put("deptNo", "部门编码");
		headAlias.put("deptType", "部门类型");
		headAlias.put("deptName", "部门名称");
		headAlias.put("deptPerf", "金额");


		//3.设置文件名称
		String fileName = "部门明细数据_" + System.currentTimeMillis();

		//4.设置数据
		List<OrmDeptVO> deptVOList = new ArrayList<>(16);
		for (int i = 0; i < deptList.size(); i++) {
			OrmDeptVO exportVO = new OrmDeptVO();
			OrmDeptVO oldVO = deptList.get(i);
			exportVO.setId(Long.valueOf(i + 1));
			exportVO.setDeptNo(oldVO.getDeptNo());
			exportVO.setDeptType(oldVO.getDeptType());
			exportVO.setDeptName(oldVO.getDeptName());
			exportVO.setDeptPerf(oldVO.getDeptPerf());
			deptVOList.add(exportVO);
		}
		try {
			ExportExcelUtils.exportExcel(response, headAlias, deptVOList, fileName);
		} catch (Exception e) {
			logger.info("导出发生异常请检查数据");
		}
	}


	/**
	 * 2. CRT数据导出
	 *
	 * @param response
	 */
	@Override
	public void exportByCrt(HttpServletResponse response) {
		List<OrmDeptVO> deptList = LocalJsonUtil.getListFromJson("json/dept.json", OrmDeptVO.class);
		//创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		outputPage(deptList, sxssfWorkbook);

		// 下载导出
		String filename = "增值服务费管理数据";
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");

		try {
			//一定要设置成xlsx格式
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
			//创建一个输出流
			ServletOutputStream outputStream = response.getOutputStream();
			//写入数据
			sxssfWorkbook.write(outputStream);

			// 关闭
			outputStream.close();
			sxssfWorkbook.close();
		} catch (Exception e) {
			logger.info("导出发生异常请检查数据");
		}
	}

	/**
	 * 输出 OrmDeptVO
	 *
	 * @param list
	 * @param sxssfWorkbook
	 */
	private void outputPage(List<OrmDeptVO> list, SXSSFWorkbook sxssfWorkbook) {
		String sheetName = "增值服务_" + System.currentTimeMillis();
		//设置样式
		CellStyle style = sxssfWorkbook.createCellStyle();
		DataFormat dataFormat = sxssfWorkbook.createDataFormat();
		style.setDataFormat(dataFormat.getFormat("0.00"));
		// 设置单元格内容水平对其方式
		style.setAlignment(HorizontalAlignment.LEFT);

		//创建sheet页
		SXSSFSheet sheet = sxssfWorkbook.createSheet(sheetName);
		//创建表头
		SXSSFRow headRow = sheet.createRow(0);

		//设置表头信息
		headRow.createCell(0).setCellValue("编码编码");
		headRow.createCell(1).setCellValue("部门类型");
		headRow.createCell(2).setCellValue("部门名称");
		headRow.createCell(3).setCellValue("金额");
		headRow.createCell(4).setCellValue("金额2");

		// 遍历上面数据库查到的数据
		for (int a = 0; a < list.size(); a++) {
			OrmDeptVO vo = list.get(a);
			//填充数据
			SXSSFRow dataRow = sheet.createRow(a + 1);
			//看你实体类在进行填充
			dataRow.createCell(0).setCellValue(vo.getDeptNo());
			dataRow.createCell(1).setCellValue(vo.getDeptType());
			dataRow.createCell(2).setCellValue(vo.getDeptName());
			setCellStyle(dataRow, 3, vo.getDeptPerf().toString(), style);
			setCellStyle(dataRow, 4, vo.getDeptPerf().toString(), style);
		}
	}

	/**
	 * 获取单元格样式
	 *
	 * @param dataRow
	 * @param columnIndex
	 * @param cellValue
	 * @param style
	 */
	private void setCellStyle(SXSSFRow dataRow, Integer columnIndex, String cellValue, CellStyle style) {
		if (StringUtils.isNotEmpty(cellValue)) {
			SXSSFCell cell = dataRow.createCell(columnIndex);
			cell.setCellValue(new BigDecimal(cellValue).doubleValue());
			cell.setCellStyle(style);
		}
	}
}
